From: Keir Fraser Date: Wed, 20 May 2009 14:29:25 +0000 (+0100) Subject: Introduce __must_be_array X-Git-Tag: archive/raspbian/4.8.0-1+rpi1~1^2~13897 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks://%22Dat/%22http:/www.example.com/cgi/%22https:/%22bookmarks:/%22Dat?a=commitdiff_plain;h=eaa024d29620e08f75090ffc90343c09176c2b12;p=xen.git Introduce __must_be_array Cloning the similar construct from Linux, allowing to detect improper uses of ARRAY_SIZE() at build time. Signed-off-by: Jan Beulich --- diff --git a/xen/include/xen/compiler.h b/xen/include/xen/compiler.h index 95295dce76..909dec0bdf 100644 --- a/xen/include/xen/compiler.h +++ b/xen/include/xen/compiler.h @@ -35,6 +35,10 @@ #define offsetof(a,b) ((unsigned long)&(((a *)0)->b)) #endif +/* &a[0] degrades to a pointer: a different type from an array */ +#define __must_be_array(a) \ + BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0]))) + #ifdef GCC_HAS_VISIBILITY_ATTRIBUTE /* Results in more efficient PIC code (no indirections through GOT or PLT). */ #pragma GCC visibility push(hidden) diff --git a/xen/include/xen/config.h b/xen/include/xen/config.h index b459e80313..9b26d882bb 100644 --- a/xen/include/xen/config.h +++ b/xen/include/xen/config.h @@ -11,7 +11,7 @@ #define EXPORT_SYMBOL(var) #define EXPORT_SYMBOL_GPL(var) -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x)) /* * The following log levels are as follows: diff --git a/xen/include/xen/lib.h b/xen/include/xen/lib.h index f5c21f64f7..ab76120da6 100644 --- a/xen/include/xen/lib.h +++ b/xen/include/xen/lib.h @@ -18,6 +18,12 @@ void __warn(char *file, int line); /* Force a compilation error if condition is true */ #define BUILD_BUG_ON(condition) ((void)sizeof(struct { int:-!!(condition); })) +/* Force a compilation error if condition is true, but also produce a + result (of value 0 and type size_t), so the expression can be used + e.g. in a structure initializer (or where-ever else comma expressions + aren't permitted). */ +#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) + #ifndef assert_failed #define assert_failed(p) \ do { \